package com.alex.insistence.web.cgform.banner.service.impl;

import java.util.List;
import java.util.UUID;

import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.oConvertUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alex.insistence.web.cgform.banner.entity.BannerEntity;
import com.alex.insistence.web.cgform.banner.entity.BannerItemEntity;
import com.alex.insistence.web.cgform.banner.service.BannerServiceI;

@Service("bannerService")
@Transactional
public class BannerServiceImpl extends CommonServiceImpl implements BannerServiceI {

  public <T> void delete(T entity) {
    super.delete(entity);
    // 执行删除操作配置的sql增强
    this.doDelSql((BannerEntity) entity);
  }

  public void addMain(BannerEntity banner, List<BannerItemEntity> bannerItemList) {
    // 保存主信息
    this.save(banner);

    /** 保存-Banner子项目 */
    for (BannerItemEntity bannerItem : bannerItemList) {
      // 外键设置
      bannerItem.setBannerId(banner.getId());
      this.save(bannerItem);
    }
    // 执行新增操作配置的sql增强
    this.doAddSql(banner);
  }

  public void updateMain(BannerEntity banner, List<BannerItemEntity> bannerItemList) {
    // 保存主表信息
    this.saveOrUpdate(banner);
    // ===================================================================================
    // 获取参数
    Object id0 = banner.getId();
    // ===================================================================================
    // 1.查询出数据库的明细数据-Banner子项目
    String hql0 = "from BannerItemEntity where 1 = 1 AND bANNER_ID = ? ";
    List<BannerItemEntity> bannerItemOldList = this.findByHql(hql0, id0);
    // 2.筛选更新明细数据-Banner子项目
    if (bannerItemList != null && bannerItemList.size() > 0) {
      for (BannerItemEntity oldE : bannerItemOldList) {
        boolean isUpdate = false;
        for (BannerItemEntity sendE : bannerItemList) {
          // 需要更新的明细数据-Banner子项目
          if (oldE.getId().equals(sendE.getId())) {
            try {
              MyBeanUtils.copyBeanNotNull2Bean(sendE, oldE);
              this.saveOrUpdate(oldE);
            } catch (Exception e) {
              e.printStackTrace();
              throw new BusinessException(e.getMessage());
            }
            isUpdate = true;
            break;
          }
        }
        if (!isUpdate) {
          // 如果数据库存在的明细，前台没有传递过来则是删除-Banner子项目
          super.delete(oldE);
        }

      }
      // 3.持久化新增的数据-Banner子项目
      for (BannerItemEntity bannerItem : bannerItemList) {
        if (oConvertUtils.isEmpty(bannerItem.getId())) {
          // 外键设置
          bannerItem.setBannerId(banner.getId());
          this.save(bannerItem);
        }
      }
    }
    // 执行更新操作配置的sql增强
    this.doUpdateSql(banner);
  }

  public void delMain(BannerEntity banner) {
    // 删除主表信息
    this.delete(banner);
    // ===================================================================================
    // 获取参数
    Object id0 = banner.getId();
    // ===================================================================================
    // 删除-Banner子项目
    String hql0 = "from BannerItemEntity where 1 = 1 AND bANNER_ID = ? ";
    List<BannerItemEntity> bannerItemOldList = this.findByHql(hql0, id0);
    this.deleteAllEntitie(bannerItemOldList);
  }

  /**
   * 默认按钮-sql增强-新增操作
   * 
   * @param id
   * @return
   */
  public boolean doAddSql(BannerEntity t) {
    return true;
  }

  /**
   * 默认按钮-sql增强-更新操作
   * 
   * @param id
   * @return
   */
  public boolean doUpdateSql(BannerEntity t) {
    return true;
  }

  /**
   * 默认按钮-sql增强-删除操作
   * 
   * @param id
   * @return
   */
  public boolean doDelSql(BannerEntity t) {
    return true;
  }

  /**
   * 替换sql中的变量
   * 
   * @param sql
   * @return
   */
  public String replaceVal(String sql, BannerEntity t) {
    sql = sql.replace("#{id}", String.valueOf(t.getId()));
    sql = sql.replace("#{create_name}", String.valueOf(t.getCreateName()));
    sql = sql.replace("#{create_by}", String.valueOf(t.getCreateBy()));
    sql = sql.replace("#{create_date}", String.valueOf(t.getCreateDate()));
    sql = sql.replace("#{update_name}", String.valueOf(t.getUpdateName()));
    sql = sql.replace("#{update_by}", String.valueOf(t.getUpdateBy()));
    sql = sql.replace("#{update_date}", String.valueOf(t.getUpdateDate()));
    sql = sql.replace("#{sys_org_code}", String.valueOf(t.getSysOrgCode()));
    sql = sql.replace("#{sys_company_code}", String.valueOf(t.getSysCompanyCode()));
    sql = sql.replace("#{wx_app_id}", String.valueOf(t.getWxAppId()));
    sql = sql.replace("#{UUID}", UUID.randomUUID().toString());
    return sql;
  }

  @Override
  public BannerEntity getAppBannerByType(String appId, String type) {

    List<BannerEntity> bannerList = findByHql(
        "select b from BannerEntity b where wxAppId=? and type=?", appId, type);
    if (bannerList == null || bannerList.isEmpty()) {
      return null;
    }
    return bannerList.get(0);
  }

}